<template>
  <div>

    <el-dialog title="新增员工" :visible.sync="showDialog" @close="cancel">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
        <el-form-item label="姓名" prop="username" label-width="120px">
          <el-input v-model="ruleForm.username" style="width:50%" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile" label-width="120px">
          <el-input v-model="ruleForm.mobile" style="width:50%" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="工号" prop="workNumber" label-width="120px">
          <el-input v-model="ruleForm.workNumber" style="width:50%" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry" label-width="120px">
          <el-date-picker
            v-model="ruleForm.timeOfEntry"
            type="datetime"
            placeholder="选择日期和时间"
            style="width:50%"
          />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment" label-width="120px">
          <el-select v-model="ruleForm.formOfEmployment" placeholder="请选择" style="width:50%">
            <el-option
              v-for="item in employeesEnum.hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName" label-width="120px">
          <el-input v-model="ruleForm.departmentName" style="width:50%" placeholder="请选择部门" />
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime" label-width="120px">
          <el-date-picker
            v-model="ruleForm.correctionTime"
            type="datetime"
            placeholder="选择日期和时间"
            style="width:50%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="sure()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import employeesEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      ruleForm: {
        username: '',
        mobile: '',
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '',
        timeOfEntry: '', // 入职时间
        correctionTime: ''// 转正时间
      },
      employeesEnum, // 聘用形式里面的值
      rules: {
        // 这里的username mobile这些名字是和表单上面的prop的名字对应 这样子就能完成表单的校验
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请输入聘用形式', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 确定
    async sure() {
      try {
        console.log(2)
        await this.$refs.ruleForm.validate() // ref对应的名字是ruleForm   写法是ref="ruleForm"
        await addEmployee(this.ruleForm)
        this.$emit('update:showDialog', false) // 向父组件传值改变showDialog的值，其中update是固定搭配，得写才能生效
        this.$parent.getEmployeeList() // 子组件调用父组件父页面的方法（调用getEmployeeList()方法）
        this.$message.success('添加成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 取消
    cancel() {
      this.$emit('update:showDialog', false)
      this.ruleForm = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.ruleForm.resetFields() // 移除校验结果
    }
  }
}
</script>
